import { _decorator, Component, Node, MeshRenderer, CCInteger, Material } from 'cc';
const { ccclass, property } = _decorator;

@ccclass('UsingEffect')
export class UsingEffect extends Component {
    @property(CCInteger)
    playTime: number = 1000;

    @property(CCInteger)
    mtIndex: number = 0;

    private _mr: MeshRenderer = null;
    private _mt: Material = null;

    private _updateTime: number = 20;
    private _startDT: number = 0;
    private _maxDT: number = 1;
    start() {
        this._mr = this.node.getComponent(MeshRenderer);
        this._mt = this._mr.getMaterial(this.mtIndex);
       
    }

    /**
     * 播放
     */
    public play() {
        this._startDT = 0;
        this.unschedule(this._mtAnimtion);
        this.schedule(this._mtAnimtion, this.playTime / 1000 / this._updateTime);
    }

    private _mtAnimtion() {
        this._startDT += this._maxDT / this._updateTime;
        if (this._startDT > this._maxDT) {
            this._startDT = this._maxDT;
            this.unschedule(this._mtAnimtion);
        }
        this._mt.setProperty("dissolveThreshold", this._startDT);
    }
}


